import cocos, pyglet
from cocos.director import director

from config import current as conf

class ControlCatcherLayer(cocos.layer.Layer):
    """Catch any control signals and don't propagate"""
    is_event_handler = True

    def __init__(self, callback = None):
        super(ControlCatcherLayer, self).__init__()
        self.callback = callback

    def on_key_press (self, k, modifiers):
        if self.callback:
            self.callback()
        return True

    def on_mouse_press (self, x, y, buttons, modifiers):
        if self.callback:
            self.callback()
        return True

    def on_mouse_motion(self, sx, sy, dx, dy):
        return True


class ClickableLabel(cocos.layer.Layer):
    """Draws a label that calls a function when clicked"""

    def __init__(self, label, callback, position=(0,0), font_size=20):
        super(ClickableLabel, self).__init__()
        self.callback = callback

        self.root = cocos.cocosnode.CocosNode()
        self.root.position = position
        self.add(self.root)

        # add a label and a drop shadow
        self.label = cocos.text.Label(label,
            font_name=conf.title_font, font_size=font_size,
            color=(255, 255, 255, 255), x=0, y=0,
            anchor_x='left', anchor_y='bottom')
        self.root.add(self.label, z=2)

        self.shadow = cocos.text.Label(label,
            font_name=conf.title_font, font_size=font_size,
            color=(0, 0, 0, 255), x=1, y=-1,
            anchor_x='left', anchor_y='bottom')
        self.root.add(self.shadow, z=1)

        self.PAD = 5

    def contains(self, point):
        x,y = point
        return (self.label.element.x - self.PAD + self.root.x <= x) and \
               (x <= (self.label.element.x + self.root.x + self.label.element.content_width + self.PAD)) and \
               (self.label.element.y + self.root.y - self.PAD <= y) and \
               (y <= (self.label.element.y + self.root.y + self.label.element.content_height + self.PAD))

    is_event_handler = True
    def on_mouse_press(self, sx, sy, buttons, modifiers):
        if buttons != pyglet.window.mouse.LEFT:
            return False

        if self.contains(director.get_virtual_coordinates(sx,sy)):
           self.callback()
           return True

    def on_mouse_motion(self, sx, sy, dx, dy):
        if self.contains(director.get_virtual_coordinates(sx,sy)):
            self.label.element.color = (100,0,0,255)
        else:
            self.label.element.color = (255,255,255,255)
        return False

class Gauge(cocos.layer.Layer):
    def __init__(self, value = lambda: .5, incr=lambda:None, decr = lambda:None,
                 position = (0,0), adjustable=True, num_bars = 50, spacing = 5, font_size = 20, col = (100,0,0,255)):
        super(Gauge, self).__init__()
        self.value_fn = value
        x,y = position
        self.color = col
        
        if adjustable:
            self.add(ClickableLabel("-", lambda: self.update(decr), (x,y)))
        x += spacing*2

        self.bars = []
        for i in range(num_bars):
            bar = cocos.text.Label("|",
                font_name=conf.title_font, font_size=font_size,
                color=(255, 255, 255, 255), x=x, y=y,
                anchor_x='left', anchor_y='bottom')
            self.bars.append(bar)
            self.add(bar)
            x += spacing
        x += spacing
        if adjustable:
            self.add(ClickableLabel("+", lambda: self.update(incr), (x,y), font_size=font_size))
        self.update()
        
    def change(self, value, color):
        self.color = color
        self.value_fn = value
        
    def update(self, f = None):
        if f: f()
        new_val = self.value_fn()
        lit_bars = int(len(self.bars)*new_val)

        for i,bar in enumerate(self.bars):
            if (i < lit_bars):
                bar.element.color = self.color
            else:
                bar.element.color = (50,50,50,255)
